﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using University.Workshop.Business;
using University.Workshop.Model;
using System.Globalization;
using System.Threading;
using University.Workshop.DataAccess.LanguagePack.ClientLanguagePack;

namespace University.Workshop.ClientApp
{
    /// <summary>
    /// Interaction logic for ForgotPassword.xaml
    /// </summary>
    public partial class ForgotPassword : Window
    {
        Validate validate = new Validate();
        Client client = new Client();
        string[] answerQuestion;
        string language;
        public ForgotPassword(string languageReceived)
        {
            InitializeComponent();
            language= languageReceived;
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(language);
            ApplyLanguage();
        }

        private void ApplyLanguage()
        {
            label8.Content = ClientLanguagePack.ForgotPasswordLabelComplete;
            labelUser.Content = ClientLanguagePack.ForgotPasswordLabelUser;
            ButtonForgot.Content = ClientLanguagePack.ForgotPasswordButtonObtain;
            label2.Content = ClientLanguagePack.ForgotPasswordLabelQuestion;
            label1.Content = ClientLanguagePack.ForgotPasswordLabelAnswer;
            buttonVerifyAnswer.Content = ClientLanguagePack.ForgotPasswordVerify;
            label3.Content = ClientLanguagePack.ForgotPasswordLabelPassword;
            label4.Content = ClientLanguagePack.ForgotPasswordLabelRetypePassword;
            ButtonOK.Content = ClientLanguagePack.ForgotPasswordButtonOK;
            buttonAgain.Content = ClientLanguagePack.ForgotPasswordButtonStart;
        }

        private string getQuestion(int index)
        {
            string question=ClientLanguagePack.ForgotPasswordServerError;
            switch (index)
            { 
                case 0:
                    question=ClientLanguagePack.ForgotPasswordQuestion1;
                    break;
                case 1:
                    question= ClientLanguagePack.ForgotPasswordQuestion2; //TODO compleate questions
                    break;
                case 2:
                    question = ClientLanguagePack.ForgotPasswordQuestion3;
                    break;
            }
            return question;


        }


        private void ButtonForgot_Click(object sender, RoutedEventArgs e)
        {
            answerQuestion=client.GetQuestionAnswer(TextBoxUser.Text);//verify if user exist in database
            if (answerQuestion.Length>1)
            {
                TextBoxUser.IsEnabled = false;
                ButtonForgot.IsEnabled = false;
                TextboxAnswer.IsEnabled = true;
                LabelQuestion.Content = getQuestion(Convert.ToInt16( answerQuestion[1]));
                labelErrorUser.Content = ClientLanguagePack.ForgotPasswordAnswer;
            }
            else
            {
                labelErrorUser.Content = ClientLanguagePack.ForgotPasswordAnswerError;
            }
        }
        private string changeTextBox(string value)
        {
            value = value.Replace("System.Windows.Controls.Textbox: ", "");
            return value;
        }
        private void TextBoxUser_TextChanged(object sender, TextChangedEventArgs e)
        {
            ButtonForgot.IsEnabled = false;
            string value = TextBoxUser.Text;
            labelErrorUser.Content = " ";
            labelErrorUser.Content = validate.ValidateUserLength(value);
            if (string.IsNullOrEmpty(value) || !validate.ValidateUserName(value,language))
            {
                labelErrorUser.Content = ClientLanguagePack.ForgotPasswordSpacesError;
            }
            string valueLabel=labelErrorUser.Content.ToString();
            if ( valueLabel== " ")
            {
                ButtonForgot.IsEnabled = true;
            }
        }

        private void TextboxAnswer_TextChanged(object sender, TextChangedEventArgs e)
        {
            labelAnswer.Content = " ";
            string value = TextboxAnswer.Text;
            if (string.IsNullOrEmpty(value))
            {
                labelAnswer.Content = ClientLanguagePack.ForgotPasswordLabelAnswerSomething;
                buttonVerifyAnswer.IsEnabled = false;
            }
            else {
                buttonVerifyAnswer.IsEnabled = true;
            }
        }

        private void buttonVerifyAnswer_Click(object sender, RoutedEventArgs e)
        {
            //consulte to database if the answer is correct
            string answer=TextboxAnswer.Text;
            if (answer.GetHashCode() == Convert.ToInt32(answerQuestion[0]))
            {
                TextboxAnswer.IsEnabled = false;
                buttonVerifyAnswer.IsEnabled = false;
                passwordBox1.IsEnabled = true;
                passwordBox2.IsEnabled = true;
                labelAnswer.Content = ClientLanguagePack.ForgotPasswordLabelAnswerCorrect;
            }
            else {
                labelAnswer.Content = ClientLanguagePack.ForgotPasswordLabelAnswerIncorrect;
            }
        }

        private void passwordBox1_PasswordChanged(object sender, RoutedEventArgs e)
        {
            labelPassword.Content = " ";
            labelRetypePassword.Content = " ";
            string value=passwordBox1.Password;
            if (!validate.ValidatePassword(value))
            {
                labelPassword.Content = ClientLanguagePack.ForgotPasswordLabelPasswordIncorrect;
                ButtonOK.IsEnabled = false;
            }
            else
            {
                labelPassword.Content = "OK";
            }
            if (passwordBox2.Password != passwordBox1.Password ||  !validate.ValidatePassword(passwordBox1.Password) && !validate.ValidatePassword(passwordBox2.Password))
            {
                labelRetypePassword.Content = ClientLanguagePack.ForgotPasswordLabelPasswordDontMatch;
                ButtonOK.IsEnabled = false;
            }
            else
            {
                labelRetypePassword.Content = "OK";
                ButtonOK.IsEnabled = true;
            }
        }

        private void passwordBox2_PasswordChanged(object sender, RoutedEventArgs e)
        {
            labelRetypePassword.Content = " ";
            string value = passwordBox2.Password;
            bool validationAnswer=validate.ValidatePassword(passwordBox1.Password);
            bool validationAnswerCopy = validate.ValidatePassword(passwordBox2.Password);
            if (validationAnswer && validationAnswerCopy)
            {
                if (value != passwordBox1.Password )
                {
                    labelRetypePassword.Content = ClientLanguagePack.ForgotPasswordLabelRetypeMatchError;
                    ButtonOK.IsEnabled = false;
                }
                else
                {
                    labelRetypePassword.Content = "OK";
                    ButtonOK.IsEnabled = true;
                }
            }
            else
            {
                labelRetypePassword.Content = ClientLanguagePack.ForgotPasswordLabelRetypeInvalid;
                ButtonOK.IsEnabled = false;
            }
            
        }

        private void buttonAgain_Click(object sender, RoutedEventArgs e)
        {
            ForgotPassword newWindow = new ForgotPassword(language);
            newWindow.Show();
            this.Close();
        }

        private void ButtonOK_Click(object sender, RoutedEventArgs e)
        {
            bool value = client.ChangePassword(TextBoxUser.Text, passwordBox1.Password.GetHashCode().ToString());
            if (value)
            {
                ButtonOK.IsEnabled = false;
                passwordBox1.IsEnabled = false;
                passwordBox2.IsEnabled = false;
                MessageBox.Show(ClientLanguagePack.ForgotPasswordAllGood);
                this.Close();
            }
            else
                labelMessagePassword.Content = ClientLanguagePack.ForgotPasswordServerError;
        }
    }
}
